﻿using Microsoft.EntityFrameworkCore;
using RuanGong.background.A.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Table_pet.background.Classes;

namespace RuanGong.background.A
{
    internal class OurDatabaseAPI
    {
        private static readonly DbContextOptions<Hcontext> contextOptions = new DbContextOptionsBuilder<Hcontext>()
               .UseMySQL("Server=localhost;Database=HDB;User=root;Password=zamm030130")
               .Options;
        public static List<health_information> readAll()
        {
            using (var context = new Hcontext(contextOptions))
            {
                var q = context.health_Information;
                return q.ToList();
            };
        }

        public static int add(health_information health_information)
        {
            using (var context = new Hcontext(contextOptions))
            {
                context.health_Information.Add(health_information);
                context.SaveChanges();
                return health_information.Id;
            }
        }

        public static health_information deleteById(int id)
        {
            using (var context = new Hcontext(contextOptions))
            {
                var heal=context.health_Information.FirstOrDefault(h => h.Id == id);
                if(heal != null)
                {
                    context.health_Information.Remove(heal);
                    context.SaveChanges();
                }
                return heal;
            }
        }
        
        public static health_information searchById(int id)
        {
            using (var context = new Hcontext(contextOptions))
            {
                var heal = context.health_Information.FirstOrDefault(h => h.Id == id);
                return heal;
            }
        }
    }
}
